// pages/user/user.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    wxUserInfo: {},
    isShowModal: false,
    showModal: false,
    marginTop: "",
    user: {}
  },
  /**
   * @method
   * @desc 获取icom联系电话
   */
  getContact() {
    wx.showModal({
      title: 'Tips',
      content: '联系我们？',
      success(res) {
        if (res.confirm) {
          wx.makePhoneCall({
            phoneNumber: '15198854094',
          })
        }
      }
    })
  },
  /**
   * @method
   * 授权回调
   */
  settingCB(res) {
    let that = this
    if (res.detail.detail.errMsg === 'getUserInfo:ok') {
      app.globalData.wxUserInfo = res.detail.detail.userInfo
      wx.showLoading({
        title: '用户登录',
        mask: true
      })
      app.getUserInfo().then(res => {
        wx.hideLoading()
        that.setData({
          showModal: false
        })
        if (res.data.code === 0) {
          //做其它操作
        } else {
          wx.showToast({
            title: '用户登录失败',
            icon: 'none'
          })
        }
      })
    } else {
      that.setData({
        showModal: false
      })
    }
  },
  /**
   * @method
   * @desc 关闭模态框
   */
  closeModal() {
    let that = this
    that.setData({
      showModal: false
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      wxUserInfo: app.globalData.wxUserInfo,
      user: app.globalData.userInfo
    })
  },
  isShowAboutModal() {
    var self = this
    this.setData({
      isShowModal: true
    })
    wx.createSelectorQuery().select('#modal').boundingClientRect(function(res) {
      self.setData({
        marginTop: (res.height) / 2
      })
    }).exec()
  },
  hideModal() {
    this.setData({
      isShowModal: false
    })
  },
  checkLoginNav: function(e) {
    if (app.globalData.userInfo) {
      wx.navigateTo({
        url: e.currentTarget.dataset.url
      })
    }
  },
  checkUserLoginNav: function(e) {
    if (app.globalData.userInfo) {
      wx.switchTab({
        url: e.currentTarget.dataset.url
      })
    }
  },
  goStore: function() {
    // var latitude = 24.9750500000
    // var longitude = 102.7891100000
    // wx.openLocation({
    //   latitude: latitude,
    //   longitude: longitude,
    //   scale: 28
    // })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    let that = this
    app.userLogin().then(res => {
      wx.hideLoading()
      if (res === 'noAuthorize') {
        that.setData({
          showModal: true
        })
      } else if (res === 'success') {
        that.setData({
          showModal: false
        })
        //做其它请求
      } else if (res === 'fail') {
        that.setData({
          showModal: false
        })
        wx.showToast({
          title: '注册用户失败',
          icon: 'none'
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})